# -*- coding: utf-8 -*-
# @Date    : 2017-05-04 15:51:53
# @Author  : fancy (fancy@thecover.co)
import unittest


def smart_sub(data, rule, m):
    try:
        if m <= 0:
            return data
        cur_rule = rule[len(rule)-m]
        tmp = data.split(cur_rule[0])
        for i in range(len(tmp)):
            tmp[i] = smart_sub(tmp[i], rule, m-1)
        return cur_rule[1].join(tmp)
    except Exception, e:
        print e


class MyTest(unittest.TestCase):

    def test_smart_sub(self):
        string = 'Japanese forces took Tulagi, '\
            'although several of their supporting warships'
        rule = [('alt', 'shit'), (' sever', 'fuck'), ('war', 'peace')]
        new_str = 'Japanese forces took Tulagi, '\
            'shithoughfuckal of their supporting peaceships'
        result = smart_sub(string, rule, len(rule))
        print result
        self.assertEqual(result, new_str)


if __name__ == '__main__':
    unittest.main()
